/**
 *  Copyright 2010 Kyle O'Connor, Justin Arling, Mike Mercieca
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package edu.drexel.sd0910.ece01.aqmon.manager;

import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

import weather_service.weatherprovider.DefaultWeatherConfigurationBean;
import weather_service.weatherprovider.WeatherConfigurationIF;
import weather_service.weatherprovider.WeatherError;
import weather_service.weatherprovider.WeatherGateway;
import weather_service.weatherprovider.locationdatamodel.SearchLocations;
import weather_service.weatherprovider.weatherdatamodel.WeatherReport;
import edu.drexel.sd0910.ece01.aqmon.config.ConfigKeys;
import edu.drexel.sd0910.ece01.aqmon.gui.MainModel;

/**
 * Manager class to manage downloading/importing weather data for EPA
 * calculations. Data source is Weather.com
 * 
 * @author Kyle O'Connor
 * 
 */
public class WeatherDataManager extends Manager {

	private final static Logger log = Logger.getLogger(WeatherDataManager.class);
	
	public final static String RAIN_CONDITIONS = "Rain";
	public static final String HEAVY_RAIN_CONDITIONS = "Heavy Rain";

	private WeatherGateway gateway;

	/**
	 * Default constructor.
	 */
	public WeatherDataManager(MainModel model, Properties props) {
		super(model, props);
		gateway = new WeatherGateway(initWeatherGateway());
	}

	private WeatherConfigurationIF initWeatherGateway() {
		String weatherServer = DefaultWeatherConfigurationBean.defaultWeatherServer;
		String partnerID = props.getProperty(ConfigKeys.WEATHER_COM_PARTNERID);
		String licenseKey = props.getProperty(ConfigKeys.WEAHTER_COM_LICENSEKEY);
		WeatherConfigurationIF config = new DefaultWeatherConfigurationBean(weatherServer, partnerID, licenseKey);
		return config;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.drexel.sd0910.ece01.aqmon.manager.Manager#shutdown()
	 */
	@Override
	public void shutdown() throws Exception {
		//nothing to do
	}

	private SearchLocations runSearchService(String loc) {
		log.debug("Searching for location IDs at: " + loc);
		SearchLocations locations = null;
		try {
			locations = gateway.searchForLocations(loc);
		} catch (WeatherError e) {
			log.error(e.toString());
		} catch (IOException e) {
			log.error(e.toString());
		}
		return locations;
	}

	public String lookupLocationID(String loc) {
		try {
			return this.runSearchService(loc).getSearchLocations().get(0).getId();
		} catch (Exception e) {
			return null;
		}
	}

	private WeatherReport runWeatherService(String locationID) {
		log.debug("Fetching weather for location: " + locationID);
		WeatherReport weather = null;
		try {
			weather = gateway.getCurrentConditions(locationID);
		} catch (WeatherError e) {
			log.error(e.toString());
		} catch (IOException e) {
			log.error(e.toString());
		}
		return weather;
	}

	/**
	 * Current Humidity measurement for resolved location.
	 * 
	 * @return humidity as <code>int</code>
	 * @throws Exception
	 *             if an error occurred obtaining weather data
	 */
	public int lookupCurrentHumidity() throws Exception {
		String locationID = model.getResolvedLocation();
		String humidity = this.runWeatherService(locationID).getCurrentConditions().getHumidity();
		return Integer.parseInt(humidity);
	}

	/**
	 * Current Temperature measurement for resolved location.
	 * 
	 * @return temperature as <code>int</code>
	 * @throws Exception
	 *             if an error occurred obtaining weather data
	 */
	public int lookupCurrentTemperature() throws Exception {
		String locationID = model.getResolvedLocation();
		String temp = this.runWeatherService(locationID).getCurrentConditions().getTemperature();
		return Integer.parseInt(temp);
	}

	/**
	 * Current description of the conditions for resolved location.
	 * 
	 * @return description as <code>String</code>
	 * @throws Exception
	 *             if an error occurred obtaining weather data
	 */
	public String lookupCurrentConditionsDescription() throws Exception {
		String locationID = model.getResolvedLocation();
		return this.runWeatherService(locationID).getCurrentConditions().getDescription();
	}

}